/**
 * 
 */
package com.ydy.service.requestLog.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ydy.exception.ValidateException;
import com.ydy.mapper.RequestLogMapper;
import com.ydy.model.RequestLog;
import com.ydy.service.requestLog.RequestLogService;
import com.ydy.utils.DateUtil;
import com.ydy.vo.RequestLogCountVo;
import com.ydy.vo.other.PageVo;

/**
 * @author xuzhaojie
 *
 *         2019年5月29日 上午8:35:26
 */
@Service
@Transactional
public class RequestLogServiceImpl implements RequestLogService {

	private final static Logger log = LoggerFactory.getLogger(RequestLogServiceImpl.class);
	@Autowired
	private RequestLogMapper requestLogMapper;

	@Value("${days:31}")
	private Long days;

	@Override
	public PageVo<RequestLog> select(RequestLog requestLog, Integer page, Integer size) {
		PageVo<RequestLog> vo = new PageVo<RequestLog>(page, size);
		Page<RequestLog> pageBean = PageHelper.startPage(vo.getPage(), vo.getSize(), "id desc");
		List<RequestLog> list = requestLogMapper.select(requestLog);
		vo.setTotal(pageBean.getTotal());
		vo.setList(list);
		return vo;
	}

	@Override
	public RequestLog saveOrUpdate(RequestLog requestLog) {
		if (requestLog == null) {
			throw new NullPointerException();
		}
		requestLogMapper.insertSelective(requestLog);
		return requestLog;
	}

	@Override
	public List<RequestLogCountVo> selectCountByDay(Date startTime, Date endTime) {
		if (startTime == null) {
			startTime = DateUtil.getBeforeOrAfterDayZeroTime(-30);
		}
		if (endTime == null) {
			endTime = new Date();
		}
		if (DateUtil.betweenDays(startTime, endTime) > days) {
			log.info("查询时间间隔过大:{}==>{}", startTime, endTime);
			Map<String, String> errors = new HashMap<String, String>();
			errors.put("error", "查询时间间隔过大，天数间隔最大值为:" + days);
			throw new ValidateException(errors);
		}
		if (endTime.before(startTime)) {
			log.info("开始时间不可大于结束时间:{}==>{}", startTime, endTime);
			throw new ValidateException("开始时间不可大于结束时间");
		}
		List<RequestLogCountVo> list = requestLogMapper.selectCountByDay(startTime, endTime);
		Long cur = startTime.getTime();
		Long end = endTime.getTime();
		HashMap<String, Integer> map = new HashMap<String, Integer>();
		String key = "";
		if (!CollectionUtils.isEmpty(list)) {
			for (RequestLogCountVo vo : list) {
				key = vo.getYear() + vo.getMonth() + vo.getDay() + "";
				map.put(key, vo.getCount());
			}
		}
		List<RequestLogCountVo> reList = new LinkedList<RequestLogCountVo>();
		Date time = null;
		RequestLogCountVo vo = null;
		for (; cur < end; cur += DateUtil.ONE_DAY_MILLONS) {
			time = new Date(cur);
			vo = new RequestLogCountVo();
			vo.setYear(DateUtil.getYear(time));
			vo.setMonth(DateUtil.getMonth(time));
			vo.setDay(DateUtil.getDay(time));
			key = vo.getYear() + vo.getMonth() + vo.getDay() + "";
			if (map.containsKey(key)) {
				vo.setCount(map.get(key));
			} else {
				vo.setCount(0);
			}
			reList.add(vo);
		}
		return reList;
	}

	@Override
	public List<RequestLogCountVo> selectIpCountByDay(Date startTime, Date endTime) {
		if (startTime == null) {
			startTime = DateUtil.getBeforeOrAfterDayZeroTime(-30);
		}
		if (endTime == null) {
			endTime = new Date();
		}
		if (DateUtil.betweenDays(startTime, endTime) > days) {
			log.info("查询时间间隔过大:{}==>{}", startTime, endTime);
			Map<String, String> errors = new HashMap<String, String>();
			errors.put("error", "查询时间间隔过大，天数间隔最大值为:" + days);
			throw new ValidateException(errors);
		}
		if (endTime.before(startTime)) {
			log.info("开始时间不可大于结束时间:{}==>{}", startTime, endTime);
			throw new ValidateException("开始时间不可大于结束时间");
		}
		List<RequestLogCountVo> list = requestLogMapper.selectCountByDay(startTime, endTime);
		Long cur = startTime.getTime();
		Long end = endTime.getTime();
		HashMap<String, Integer> map = new HashMap<String, Integer>();
		String key = "";
		if (!CollectionUtils.isEmpty(list)) {
			for (RequestLogCountVo vo : list) {
				key = vo.getYear() + vo.getMonth() + vo.getDay() + "";
				map.put(key, vo.getCount());
			}
		}
		List<RequestLogCountVo> reList = new LinkedList<RequestLogCountVo>();
		Date time = null;
		RequestLogCountVo vo = null;
		for (; cur < end; cur += DateUtil.ONE_DAY_MILLONS) {
			time = new Date(cur);
			vo = new RequestLogCountVo();
			vo.setYear(DateUtil.getYear(time));
			vo.setMonth(DateUtil.getMonth(time));
			vo.setDay(DateUtil.getDay(time));
			key = vo.getYear() + vo.getMonth() + vo.getDay() + "";
			if (map.containsKey(key)) {
				vo.setCount(map.get(key));
			} else {
				vo.setCount(0);
			}
			reList.add(vo);
		}
		return reList;
	}

	@Override
	public List<RequestLogCountVo> selectCountByHour(Date date) {
		if (date == null) {
			date = new Date();
		}
		Integer year = DateUtil.getYear(date);
		Integer month = DateUtil.getMonth(date);
		Integer day = DateUtil.getDay(date);
		List<RequestLogCountVo> list = requestLogMapper.selectCountByHour(year, month, day);
		Map<Integer, Integer> map = new HashMap<Integer, Integer>();
		String url = "";
		for (RequestLogCountVo data : list) {
			map.put(data.getHour(), data.getCount());
			if (StringUtils.isEmpty(url)) {
				url = data.getUrl();
			}
		}
		List<RequestLogCountVo> resultList = new LinkedList<RequestLogCountVo>();
		RequestLogCountVo temp = null;
		int count = 0;
		for (int hour = 0; hour < 24; hour++) {
			count = 0;
			temp = new RequestLogCountVo();
			if (map.containsKey(hour)) {
				count = map.get(hour);
			}
			temp.setCount(count);
			temp.setDay(day);
			temp.setHour(hour);
			temp.setYear(year);
			temp.setMonth(month);
			temp.setUrl(url);
			resultList.add(temp);
		}
		return resultList;
	}

}
